# $Id: PKGBUILD 174925 2013-01-09 04:17:20Z andyrtr $
# Maintainer: Tobias Powalowski <tpowa@archlinux.org>
#Contributed: eliott <eliott@solarblue.net>, Andre Naumann <anaumann@SPARCed.org> 

pkgname=freenx
pkgver=0.7.3
pkgrel=15
pkgdesc="Free Software (GPL) Implementation of the NX Server"
arch=(i686 x86_64)
url="http://freenx.berlios.de"
license=('GPL')
depends=('nx-common' 'nx-x11>=3.5.0.17' 'nxagent' 'nxproxy' 'netcat' 'inetutils' 'openssh' 'expect' 'python2' 'python2-gobject'
         'xorg-xauth' 'xorg-fonts-misc' 'coreutils' 'xorg-xmessage' 'xorg-xrdb' 'xorg-xpr' 'xorg-xset' 'xorg-sessreg')
optdepends=('cups: adds printing support')
makedepends=('imake')
options=('!makeflags')
conflicts=('nxserver')
provides=('nxserver')
backup=(etc/nxserver/node.conf)
install=freenx.install
source=(http://download.berlios.de/freenx/$pkgname-server-$pkgver.tar.gz
        keymap.patch
        nxagent-startup.patch
        freenx-latest-svn.patch
        remove-dialog.patch
        freenx-server-0.7.3-nxipp.patch)
md5sums=('856f597e139018f7ed62713c9d6c9ed5'
         'f0867659c925f6363fb2b7661f349248'
         '30dc16d8e2093ff71d5834a169ad9d00'
         '788f30322783cf2c2dc833e2e4d5e8ad'
         'cae1773312506eeefe6a8de07c492bd8'
         'b3244c52a6bafc6d9b528eb4d4426e36')

build() {
  # patch and install nxserver
  cd $srcdir/$pkgname-server-$pkgver
  # add latest svn fixes
  patch -Np3 -i ../freenx-latest-svn.patch
  # patch from inside nx
  patch < gentoo-nomachine.diff 
  # patch broken keymap remapping
  patch -Np0 -i ${srcdir}/keymap.patch 
  # patch nxagent startup to avoid login issues
  patch -Np0 -i ${srcdir}/nxagent-startup.patch
  # check for cups existance and then symlinks the backend
  patch -Np1 -i ${srcdir}/freenx-server-0.7.3-nxipp.patch
  # remove broken dialog function
  patch -Np0 -i  ${srcdir}/remove-dialog.patch
  sed -i -e 's,authorized_keys2,authorized_keys,g' nxloadconfig
  sed -i -e 's,\/usr\/NX,\/usr\/lib/nx,g' nxloadconfig node.conf.sample
  sed -i -e "s:NX_DIR\/lib:NX_DIR\/X11:" nxloadconfig
  sed -i -e 's,netcat,nc,g' nxloadconfig
  sed -i -e 's/utmp/nx/g' nxsetup
  sed -i -e 's,/usr/X11R6/bin/xauth,/usr/bin/xauth,g' nxloadconfig
  sed -i -e 's,COMMAND_FOOMATIC="/usr/lib/cups/driver/foomatic-ppdfile",COMMAND_FOOMATIC="/usr/bin/foomatic-ppdfile",g' nxloadconfig
  sed -i -e 's,COMMAND_MD5SUM="openssl md5",COMMAND_MD5SUM="md5sum",g' nxloadconfig node.conf.sample
  sed -i -e 's|AGENT_EXTRA_OPTIONS_X=""|AGENT_EXTRA_OPTIONS_X="-co /usr/share/X11/rgb -fp /usr/share/fonts/misc,/usr/share/fonts/75dpi,/usr/share/fonts/100dpi,/usr/share/fonts/TTF,/usr/share/fonts/Type1 -xkbdir /usr/share/X11/xkb"|g' nxloadconfig
  sed -i -e 's/ENABLE_AUTORECONNECT_BEFORE_140="1"/ENABLE_AUTORECONNECT_BEFORE_140="0"/g' nxloadconfig
  sed -i -e 's,/etc/init.d/ssh,/etc/rc.d/sshd,g' nxsetup
  sed -i -e 's,libXcomp.so,libXcomp.so.3,g' nxloadconfig
  sed -i -e 's,libXcompext.so,libXcompext.so.3,g' nxloadconfig
  sed -i -e 's,libXrender.so.1.2,libXrender.so.1.2.2:$APPLICATION_LIBRARY_PATH/libXcompsh.so.3:$APPLICATION_LIBRARY_PATH/libXcompshad.so.3,g' nxloadconfig
  sed -i -e 's:NX_ETC_DIR\=$NX_DIR\/etc:NX_ETC_DIR=\/etc\/nxserver:g' nxloadconfig
  sed -i -e 's:NX_SESS_DIR=$NX_DIR\/var\/db:NX_SESS_DIR=\/var\/lib\/nxserver\/db:g' nxloadconfig
  sed -i -e 's:NX_HOME_DIR=$NX_DIR:NX_HOME_DIR=\/var\/lib\/nxserver:g' nxloadconfig
  sed -i -e 's:NOMACHINE_NX_HOME_DIR="/usr/lib/nx/home/nx":NOMACHINE_NX_HOME_DIR="/var/lib/nxserver/home/nx":g' nxloadconfig node.conf.sample
  sed -i -e 's:\/usr\/NX:\/usr\/lib\/nx:g' nxdialog

  # python2 fix
  sed -i -e 's:\/usr\/bin\/env\ python:\/usr\/bin\/env\ python2:' nx-session-launcher/nx-session-launcher

  # fix key generation
  sed -i -e 's,AUTOMATIC="no",AUTOMATIC="yes",g' nxsetup

  make
}

package() {
  cd $srcdir/$pkgname-server-$pkgver  

  make install DESTDIR="${pkgdir}"

  # create symlinks
  install -dm755 ${pkgdir}/usr/bin
  cd ${pkgdir}/usr/bin
  for file in ${pkgdir}/usr/lib/nx/bin/*; do
    ln -sv /usr/lib/nx/bin/`basename $file` .
  done

  mv $pkgdir/etc/nxserver/node.conf.sample $pkgdir/etc/nxserver/node.conf
 
  # create some missing nxserver directories
  mkdir -m700 -p $pkgdir/var/lib/nxserver/{home/nx,db}
  mkdir -m700 -p $pkgdir/usr/lib/nx/share

  # remove files that are part of nxclient
  rm -f  ${pkgdir}/usr/lib/nx/bin/nxprint
  rm -f  ${pkgdir}/usr/bin/nxprint
}
